import Vue from 'vue'
import login from '../biz/login'
import Router from 'vue-router'
import Main from '../page/Main.vue'
import Default from '../page/Main/Default.vue'
import Login from '../page/Login.vue'

Vue.use(Router)

const router = new Router({
    routes: [
        { path: '/',
            redirect: to => {
                return '/main/default'
            }
        },{
            path: '/main/',
            name: 'main',
            component: Main,
            redirect: to => {
                return '/main/default'
            },
            children: [
                {
                    path: 'default',
                    component: Default,
                    meta:{
                        hasToken: true
                    },
                },
            ]
        },{
            path: '/login',
            name: 'Login',
            component: Login
        }
    ]
})
export default router

/**
 * 登录页面
 */
router.beforeEach((to, from, next) => {
    if (to.meta && to.meta.hasToken) {
        if (!login.isLoggedIn()) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } else {
            next()
        }
    } else {
        next()
    }
})
